/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxHandlerController
 * @Package com.rx.core.handler
 * @Description: 处理器控制类
 * @author: 陈锦韬
 * @date: 2021\6\14 0014
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.handler;

import com.rx.core.mode.HandlerContainer;
import lombok.Data;

import java.util.*;

/**
 * @author: Administrator
 * @Description: 处理器控制类
 * 1.可以临时分别控制是否让前，覆盖，后处理器失效。
 * 2.可以临时增删指定处理器
 * 3.控制固定逻辑是否失效
 * @date: 2021\6\14 0014
 */
@Data
public class RxHandlerController {
    Boolean enablePrev = true;
    Boolean enableOver = true;
    Boolean enableAfter = true;
    Boolean enableDefault = true;

    Map<String,Integer> appendPrevHandler=new HashMap<>();
    Map<String,Integer> appendOverHandler=new HashMap<>();
    Map<String,Integer> appendAfterHandler=new HashMap<>();
    List<String> removePrevHandler=new ArrayList<>();
    List<String> removeOverHandler=new ArrayList<>();
    List<String> removeAfterHandler=new ArrayList<>();

    // 跳过n个处理器
    Integer skipLength = 0;
    // 循环处理n次
    Integer repeatLength = 0;

    public static RxHandlerController create(){
        return new RxHandlerController();
    }
    public static RxHandlerController createChangeAll(){
        RxHandlerController rxHandlerController = new RxHandlerController();
        rxHandlerController.enableAfter = false;
        rxHandlerController.enableOver = false;
        rxHandlerController.enablePrev = false;
        return rxHandlerController;
    }
    public static RxHandlerController createChangePrev(){
        RxHandlerController rxHandlerController = new RxHandlerController();
        rxHandlerController.enablePrev = false;
        return rxHandlerController;
    }
    public static RxHandlerController createChangeOver(){
        RxHandlerController rxHandlerController = new RxHandlerController();
        rxHandlerController.enableOver = false;
        return rxHandlerController;
    }
    public static RxHandlerController createChangeAfter(){
        RxHandlerController rxHandlerController = new RxHandlerController();
        rxHandlerController.enableAfter = false;
        return rxHandlerController;
    }

    public RxHandlerController appendPrev(String handlerName,Integer order){
        appendPrevHandler.put(HandlerContainer.getHandlerName(handlerName),order);
        return this;
    }
    public RxHandlerController appendOver(String handlerName,Integer order){
        appendOverHandler.put(HandlerContainer.getHandlerName(handlerName),order);
        return this;
    }
    public RxHandlerController appendAfter(String handlerName,Integer order){
        appendAfterHandler.put(HandlerContainer.getHandlerName(handlerName),order);
        return this;
    }
    public RxHandlerController appendPrev(String handlerName){
        return appendPrev(handlerName,null);
    }
    public RxHandlerController appendOver(String handlerName){
        return appendOver(handlerName,null);
    }
    public RxHandlerController appendAfter(String handlerName){
        return appendAfter(handlerName,null);
    }
    public RxHandlerController removePrev(String handlerName){
        removePrevHandler.add(HandlerContainer.getHandlerName(handlerName));
        return this;
    }
    public RxHandlerController removeOver(String handlerName){
        removeOverHandler.add(HandlerContainer.getHandlerName(handlerName));
        return this;
    }
    public RxHandlerController removeAfter(String handlerName){
        removeAfterHandler.add(HandlerContainer.getHandlerName(handlerName));
        return this;
    }

    /**
     * 获取删除的处理器
     * @param cls
     * @return
     */
    public List<String> getForRemove(Class cls){
        if(RxHandler.class.equals(cls)){
            return removeOverHandler;
        }else if(RxHandlerPrev.class.equals(cls)){
            return removePrevHandler;
        }else if(RxHandlerAfter.class.equals(cls)){
            return removeAfterHandler;
        }
        return Collections.emptyList();
    }
    public Map<String,Integer> getForAppend(Class cls){
        if(RxHandler.class.equals(cls)){
            return appendOverHandler;
        }else if(RxHandlerPrev.class.equals(cls)){
            return appendPrevHandler;
        }else if(RxHandlerAfter.class.equals(cls)){
            return appendAfterHandler;
        }
        return Collections.emptyMap();
    }

    public RxHandlerController stopPrev(){
        enablePrev = false;
        return this;
    }
    public RxHandlerController stopOver(){
        enableOver = false;
        return this;
    }
    public RxHandlerController stopAfter(){
        enableAfter = false;
        return this;
    }
    public RxHandlerController stopDefault(){
        enableDefault = false;
        return this;
    }

    /**
     * 去掉处理器。只运行默认代码
     * @return
     */
    public RxHandlerController onlyDefault(){
        stopOver();
        stopAfter();
        stopPrev();
        enableDefault = true;
        return this;
    }
}
